package com.oddfar.campus.business.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.oddfar.campus.business.domain.Tcwxx;
import com.oddfar.campus.business.domain.Tcwxx;
import com.oddfar.campus.business.domain.Tcwxx;
import com.oddfar.campus.common.core.BaseMapperX;
import com.oddfar.campus.common.core.LambdaQueryWrapperX;
import com.oddfar.campus.common.domain.PageResult;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface TcwxxMapper extends BaseMapperX<Tcwxx> {

    default PageResult<Tcwxx> selectPage(Tcwxx tcwxx) {

        return selectPage(new LambdaQueryWrapperX<Tcwxx>()
                .eqIfPresent(Tcwxx::getTcwid, tcwxx.getTcwid())
                .likeIfPresent(Tcwxx::getTcw, tcwxx.getTcw())
                .eq(Tcwxx::getDelFlag, false)
        );

    }

    default PageResult<Tcwxx> selectPage(Tcwxx tcwxx, Long tcwid) {

        return selectPage(new LambdaQueryWrapperX<Tcwxx>()
                .eqIfPresent(Tcwxx::getTcwid, tcwxx.getTcwid())
                .likeIfPresent(Tcwxx::getTcw, tcwxx.getTcw())
        );

    }

    @Delete("DELETE FROM dj_tcwxx WHERE tcwid in (#{tcwid})")
    int deleteTcwxx(Long tcwid);


    @Update("UPDATE dj_tcwxx SET dqkybz = 0 WHERE tcwid = #{tcwid}")
    void setDqkybzEqZero(Long tcwid);

    /*
    * xxl-job定时任务，判断预约时间是否结束，将当前可用标志为0改为1
     */

    @Update("UPDATE dj_tcwxx\n" +
            "SET dqkybz = 1\n" +
            "WHERE tcwid IN (SELECT tcwid\n" +
            "                FROM cl_tcw\n" +
            "                WHERE CURRENT_TIMESTAMP BETWEEN kssj AND jssj)")
    void updateDqkybzEqOne();
}